﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Web;


namespace Zen.Toolkit
{
    [ToolboxData("<{0}:Form runat=\"server\"></{0}:Form>")]
    [DojoWidget(ClassMapping = "dijit.form.Form", InvokeStartup = false)]
    [DojoRequires(ModuleName = "dijit.form.Form")]
    public class Form : WidgetBase
    {
        public Form() : base(HtmlTextWriterTag.Form) { }

        protected override ControlCollection CreateControlCollection()
        {
            return new ControlCollection(this);
        }

        protected override void LoadViewState(object savedState)
        {
            if (savedState != null)
            {
                base.LoadViewState(savedState);
                string text = (string)this.ViewState["innerhtml"];
                if (text != null)
                {
                    this.Controls.Clear();
                    this.Controls.Add(new LiteralControl(text));
                }
            }
        }

        //protected internal override void Render(HtmlTextWriter writer)
        //{
        //    this.RenderBeginTag(writer);
        //    this.RenderChildren(writer);
        //    this.RenderEndTag(writer);
        //}

        //protected override void RenderAttributes(HtmlTextWriter writer)
        //{
        //    this.ViewState.Remove("innerhtml");
        //    base.RenderAttributes(writer);
        //}

        //protected virtual void RenderEndTag(HtmlTextWriter writer)
        //{
        //    writer.WriteEndTag(this.TagName);
        //}

        // Properties
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public virtual string InnerHtml
        {
            get
            {
                if (base.IsLiteralContent())
                {
                    return ((LiteralControl)this.Controls[0]).Text;
                }
                if ((this.HasControls() && (this.Controls.Count == 1)) && (this.Controls[0] is DataBoundLiteralControl))
                {
                    return ((DataBoundLiteralControl)this.Controls[0]).Text;
                }

                return string.Empty;
            }
            set
            {
                this.Controls.Clear();
                this.Controls.Add(new LiteralControl(value));
                this.ViewState["innerhtml"] = value;
            }
        }

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public virtual string InnerText
        {
            get
            {
                return HttpUtility.HtmlDecode(this.InnerHtml);
            }
            set
            {
                this.InnerHtml = HttpUtility.HtmlEncode(value);
            }
        }


    }
}
